// Copyright (C) 2009 Jesse Jones
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;

internal abstract class Shape
{
}

internal sealed class Square : Shape
{
	[Intersects7.Overload]
	public static bool Intersects(Square lhs, Shape rhs)
	{
		return true;
	}
	
	[Intersects7.Overload]
	public static bool Intersects(Shape lhs, Square rhs)
	{
		return true;
	}
}

internal sealed class Disk : Shape
{
}

internal static class Program
{
	public static void Main(string[] args)
	{
		var square = new Square();
		var disk = new Disk();
		DoCheck(disk, disk, "Invoke failed to find an overload for Intersects7(Disk, Disk).");
		DoCheck(square, square, "Invoke found ambiguous overloads for Intersects7(Square, Square).");
	}
	
	private static void DoCheck(Shape lhs, Shape rhs, string expected)
	{
		try
		{
			Intersects7.Invoke(lhs, rhs);
			throw new Exception(string.Format("Expected an exception for ({0}, {1})", lhs.GetType(), rhs.GetType()));
		}
		catch (MissingMethodException e)
		{
			if (e.Message != expected)
				throw new Exception(string.Format("Expected '{0}', but got '{1}'.", expected, e.Message));
		}
		catch (System.Reflection.AmbiguousMatchException e)
		{
			if (e.Message != expected)
				throw new Exception(string.Format("Expected '{0}', but got '{1}'.", expected, e.Message));
		}
	}
}
